package ru.ganyaev.utils;

import java.nio.file.Path;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class WindowsScriptGenerator extends ScriptGenerator {
    private static final Logger LOGGER = LogManager.getRootLogger();

    @Override
    public String generateScriptText(String processName,
            List<String> arguments, Path processFolder) {
        String scriptText = frame(processName) + " ";

        for (String arg : arguments) {
            scriptText = scriptText + frame(arg) + " ";
        }

        scriptText = scriptText
                + "1>"
                + frame(processFolder.resolve(getStdOutFileName()).toString()
                        + " ")
                + " 2>"
                + frame(processFolder.resolve(getStdErrorFileName()).toString()
                        + " ");
        LOGGER.debug("Generated script: '" + scriptText + "'.");
        return scriptText;
    }

    private String frame(String string) {
        if (string.contains(" ")) {
            return "\\\"" + string + "\\\"";
        } else {
            return string;
        }
    }
}
